﻿#include "utils/pathmanager.h"
#include "common/defines.h"


#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QCoreApplication>

#ifdef Q_OS_WIN
#include <shlobj.h>
#include <windows.h>
#endif


namespace Utility {

PathManager *PathManager::m_instance = NULL;

PathManager::PathManager(QObject *parent) :
    QObject(parent)
{
    init();
}


PathManager::~PathManager()
{
    delete m_instance;
    m_instance = NULL;
}


void PathManager::init()
{
    initBasicPaths();
}


PathManager *PathManager::instance()
{
    if (!m_instance)
        m_instance = new PathManager();
    return m_instance;
}


QString PathManager::homePath()
{
#ifdef Q_OS_WIN
    wchar_t homepath[MAX_PATH];

    // There is unfortunately no way to get this path using Qt4 API.
    if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, homepath)))
        return QDir(QString::fromWCharArray(homepath)).canonicalPath();
#endif

    return QDir::homePath();
}


void PathManager::initBasicPaths()
{
    /*
     * TODO .......................
     */

#ifdef Q_WS_X11
    m_desktopFilePath = QCoreApplication::applicationDirPath() + QLatin1String("/");
    m_desktopFilePath = QFileInfo(DesktopFilePath).canonicalFilePath();
#endif

#ifdef Q_OS_MAC
    // TODO: Remove this OS X-specific code. Needs small changes to CMake and create_macosx_bundle.sh scripts.
    m_dataPath = QCoreApplication::applicationDirPath() + QLatin1String("");
#else
    m_dataPath = QCoreApplication::applicationDirPath() + QLatin1String("/");
#endif
    QString canonicalPath = QDir(m_dataPath).canonicalPath();
    if (!canonicalPath.isEmpty())
        m_dataPath = canonicalPath + '/';

#ifdef Q_OS_MAC
    // TODO: Remove this OS X-specific code. Needs small changes to CMake and create_macosx_bundle.sh scripts.
    m_pluginsLibPath = QCoreApplication::applicationDirPath() + QLatin1String("/");
#else
    m_pluginsLibPath = QCoreApplication::applicationDirPath() + QLatin1String("/");
#endif
    canonicalPath = QDir(m_pluginsLibPath).canonicalPath();
    if (!canonicalPath.isEmpty())
        m_pluginsLibPath = canonicalPath + '/';
}

} // namespace Utility
